import { reqregister, reqcode, reqlogin,reqgetuserlist } from '@/api'

// search 模块的小仓库
const state = {
    code: '',
    name:""
}
const mutations = {
    DISGETCODE(state, rel) {
        state.code = rel
    },
    GETUSERINFO(state, rel){
        state.name = rel
    }
}
const actions = {
    // 获取验证码
    async disgetcode({ commit }, phone) {
        let result = await reqcode(phone)
        if (result.code == 200) {
            console.log(result.data);
            commit("DISGETCODE", result.data)
        }
    },
    // 注册
    async disregister({ commit }, data) {
        let result = await reqregister(data)
        if (result.code == 200) {
            return 'ok';
        } else {
            //注册失败
            return Promise.reject(new Error(result.message));
        }
    },
    // 登录
    async disreqlogin({ commit }, data) {
        let result = await reqlogin(data)
        if (result.code == 200) {
            localStorage.setItem('token', result.data.token);
            return 'ok';
        } else {
            // 登陆失败
            return Promise.reject(new Error(result.message));
        }
    },
    // 获取用户信息
    async disreqgetuserlist({commit}){
        let result = await reqgetuserlist()
        if(result.code == 200) {
            commit("GETUSERINFO", result.data.name)
        }
    }
}
const getters = {

}
export default {
    state,
    mutations,
    actions,
    getters
}